<template>
  <div class="table-box">
    <ProTable
      ref="proTableRef"
      title="字典"
      :indent="20"
      :columns="columns"
      :search-columns="searchColumns"
      :request-api="getTableList"
      row-key="id"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary"
          v-auth="'novel.dict.create'"
          :icon="CirclePlus"
          @click="openAddEdit('新增字典')"
        >
          新增
        </el-button>
        <el-button
          v-auth="'novel.dict.remove'"
          type="danger"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
<!--        <el-button-->
<!--          v-auth="'novel.dict.import'"-->
<!--          type="primary"-->
<!--          :icon="Upload"-->
<!--          plain-->
<!--          @click="importData"-->
<!--        >-->
<!--          导入-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          v-auth="'novel.dict.export'"-->
<!--          type="primary"-->
<!--          :icon="Download"-->
<!--          plain-->
<!--          @click="downloadFile"-->
<!--        >-->
<!--          导出-->
<!--        </el-button>-->
      </template>
      <template #operation="{ row }">
        <el-button
          v-auth="'novel.dict.update'"
          type="primary"
          link
          :icon="EditPen"
          @click="openAddEdit('编辑字典', row, false)"
        >
          编辑
        </el-button>
        <el-button
            v-auth="'novel.dict.remove'"
          type="primary"
          link
          :icon="Delete"
          @click="deleteInfo(row)"
        >
          删除
        </el-button>
      </template>
    </ProTable>
    <NovelDictForm ref="novelDictRef" />
    <ImportExcel ref="ImportExcelRef" />
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import {
  CirclePlus,
  Delete,
  EditPen,
  Upload,
  Download,
} from '@element-plus/icons-vue'
import ProTable from '@/components/ProTable/index.vue'
import {
  createNovelDictApi,
  removeNovelDictApi,
  updateNovelDictApi,
  getNovelDictListApi,
  importNovelDictExcelApi,
  exportNovelDictExcelApi, getNovelDictApi, getNovelDictDetailApi,
} from '@/api/modules/novel/novelDict';
import { useHandleData } from '@/hooks/useHandleData';
import NovelDictForm from '@/views/novel/dict/components/dict.vue';
import type { ColumnProps, ProTableInstance, SearchProps } from '@/components/ProTable/interface';
import type { NovelDictQuery, NovelDictRow } from '@/api/types/novel/novelDict';
import ImportExcel from '@/components/ImportExcel/index.vue';
import { downloadTemplate } from '@/api/modules/system/common';
import { useDownload } from "@/hooks/useDownload";
defineOptions({
  name: 'NovelDictView'
})
const proTableRef = ref<ProTableInstance>();
// 表格配置项
const columns: ColumnProps<NovelDictRow>[] = [
  { type: 'selection', width: 80 },
  { prop: 'name', label: '字典名称' },
  { prop: 'code', label: '编码' },
  { prop: 'operation', label: '操作', width: 250, fixed: 'right' }
]
// 搜索条件项
const searchColumns: SearchProps[] = [
  { prop: 'name', label: '字典名称', el: 'input' },
  { prop: 'code', label: '编码', el: 'input' },
]
// 获取table列表
const getTableList = (params: NovelDictQuery) => {
  let newParams = formatParams(params);
  return getNovelDictListApi(newParams);
};

const formatParams = (params: NovelDictQuery) =>{
  let newParams = JSON.parse(JSON.stringify(params));
  if(newParams.updateTime) {
    newParams.updateTimeStart = newParams.updateTime[0];
    newParams.updateTimeEnd = newParams.updateTime[1];
    delete newParams.updateTime;
  }

  if(newParams.createTime) {
    newParams.createTimeStart = newParams.createTime[0];
    newParams.createTimeEnd = newParams.createTime[1];
    delete newParams.createTime;
  }

  return newParams;
}
// 打开 drawer(新增、查看、编辑)
const novelDictRef = ref<InstanceType<typeof NovelDictForm>>()
const openAddEdit = async(title: string, row: any = {}, isAdd = true) => {
  if (!isAdd) {
    const record = await getNovelDictApi({id: row?.id});
    row = record?.data
    const detailData = await getNovelDictDetailApi({id: row?.id});
    row.details = detailData?.data?.rows ?? []
  }

  const params = {
    title,
    row: { ...row },
    api: isAdd ? createNovelDictApi : updateNovelDictApi,
    getTableList: proTableRef.value?.getTableList,
    isAdd : isAdd
  }
  novelDictRef.value?.acceptParams(params)
}
// 删除信息
const deleteInfo = async (params: NovelDictRow) => {
  await useHandleData(
    removeNovelDictApi,
    { ids: [params.id] },
    `删除【${params.id}】字典`
  )
  proTableRef.value?.getTableList()
}
// 批量删除信息
const batchDelete = async (ids: (string | number)[]) => {
  await useHandleData(removeNovelDictApi, { ids }, '删除所选字典')
  proTableRef.value?.clearSelection()
  proTableRef.value?.getTableList()
}
// 导入
const ImportExcelRef = ref<InstanceType<typeof ImportExcel>>()
const importData = () => {
  const params = {
    title: '字典',
    templateName: '字典',
    tempApi: downloadTemplate,
    importApi: importNovelDictExcelApi,
    getTableList: proTableRef.value?.getTableList
  }
  ImportExcelRef.value?.acceptParams(params)
}
// 导出
const downloadFile = async () => {
  let newParams = formatParams(proTableRef.value?.searchParam as NovelDictQuery);
  useDownload(exportNovelDictExcelApi, "字典", newParams);
};

</script>